/**
 * This file is a part of qloudgen-rul. 
 * You can redistribute qloudgen-rul and/or modify it under the terms of the Lesser GNU General Public License version 3. 
 * qloudgen-rul is distributed WITHOUT ANY WARRANTY. 
 *
 * See the Lesser GNU General Public License for more details at http://www.gnu.org/licenses/. 
 **/
package org.qloudgen.rul.drools.rule;

import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <p>Title: Drools Implementation</p>
 * <p>Description: Wrappered jboss drools rules engine for classpath project implementation.</p>
 * <p>Copyright: qloudgen/rul 2014</p>
 * @author <a href="mailto:hyq.dd.sun@gmail.com">glue.3M</a>
 */
public class DroolsClasspathRuleRunner extends AbstractDroolsRuleRunner {
	/**
	 * Log output.
	 */
	private final static Logger logger = LoggerFactory.getLogger( DroolsClasspathRuleRunner.class );
	/**
	 *
	 */
	private KieContainer container = null;
	
	/**
	 * Default constructor.
	 */
	public DroolsClasspathRuleRunner() {
	}
	
	/**
	 * Initialize.
	 */
	public void init() {
		/**
		 * Default kie service factory using singleton pattern..
		 *
		 * @see KieServicesImpl.getKieClasspathContainer(), a global singleton.
		 * @see KieServicesImpl.newKieClasspathContainer(), new instance.
		 */
		container = KieServices.Factory.get().getKieClasspathContainer();
		logger.info( "Started initialize rule runner...{}" , container );
	}
	
	/**
	 * Destroy.
	 */
	public void destroy() {
		container = null;
		logger.info( "Started destroy rule runner..." );
	}
	
	/**
	 * Returns current container by session name.
	 *
	 * @param sessionName
	 * @return KieContainer
	 */
	protected KieContainer getContainer(String sessionName) {
		return container;
	}
	
	/**
	 * Returns the String representation.
	 *
	 * @return String
	 */
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append( "[" );
		builder.append( super.toString() );
		builder.append( "-->" );
		builder.append( "container=" ).append( container );
		builder.append( "]" );
		//
		return builder.toString();
	}
}